package cn.gx.learn.security_boot_jwt_websocket.base.controller;

import cn.gx.learn.security_boot_jwt_websocket.base.dal.database.User;
import cn.gx.learn.security_boot_jwt_websocket.base.service.user.UserService;
import com.nimbusds.jose.JOSEException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/login")
    public ResponseEntity<String> login(@RequestParam String username, @RequestParam String password) {
        try {
            String s = userService.uerLogin(username, password);
            return ResponseEntity.status(200).body(s);
        } catch (JOSEException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestParam String username,@RequestParam String password){
        User user = new User(username,password);
        try {
            String i=userService.userRegister(user);
            return ResponseEntity.ok(i);
        } catch (JOSEException e) {
            throw new RuntimeException(e);
        }

    }

    @GetMapping("/hellow")
    public ResponseEntity<String> hellow(){
        return ResponseEntity.ok("success");
    }

}
